package qwqw;

// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.*;
import java.sql.ResultSet;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;


import Entity.*;

import Entity.*;

import Entity.User;
import qwqw.ScheduledTask;

import ocsf.server.*;

/**
 * This class overrides some of the methods in the abstract superclass in order
 * to give more functionality to the server.
 * 
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;re
 * @author Fran&ccedil;ois B&eacute;langer
 * @author Paul Holden
 * @version July 2000
 */
public class EchoServer extends AbstractServer {

	//private Timer timer;
	//private ScheduledTask onceADayTask;

//	private Timer timer;
	//private ScheduledTask onceADayTask;

	private mysqlConnection sqlc;
	private Object result;

	//Constructors ****************************************************

	/**
	* Constructs an instance of the server.
	*
	* @param port The port number to connect on.
	*/
	public EchoServer(int port) 
	{
		super(port);
		
		sqlc=new mysqlConnection();
		result=null;

		//onceADayTask  = new ScheduledTask();
	//	timer = new Timer();

	//	onceADayTask  = new ScheduledTask();
		//timer = new Timer();

		/** Setting the task to run once a day starting in 5 minutes */
		//timer.scheduleAtFixedRate(onceADayTask, 5*60*1000, 24*60*60*1000);

	}


	//Instance methods ************************************************

	/**
	* This method handles any messages received from the client.
	* It redirects it to the database class and receives an answer as
	* an object and sends it back to client
	*
	* @param msg The object message received from the client.
	* @param client The connection from which the message originated.
	*/
	public void handleMessageFromClient(Object msg, ConnectionToClient client) {
		MsgPackage new_msg= (MsgPackage)msg;
		
		switch (new_msg.getOp_code()){
		case ADD_REQUEST:
			Request req=(Request)new_msg.getObject();
			result=sqlc.addRequest(req);
			break;
			
		/*case DISPLAY_REPORT:   // in case that the user want to display the report 
			String disReportArry[] = ( String[])new_msg.getObject(); // casting the object to array  [0] = start , [1] = end 
			result = sqlc.dis_Report(disReportArry); // result will get the table back from the the data base 
			break;*/
		
		case PERFORMANCE_REPORT: // in case that the user want to display the performance report 
		Parformance_report p_r = ( Parformance_report)new_msg.getObject(); // casting the object to array  [0] = start , [1] = end 
		result = sqlc.getParformanceReportQuery(p_r ); // result will get the table back from the the data base 
		break;
		
		case TARDINESS_REPORT: // in case that the user want to display the tardiness  report 
			Tardiness_report tar_rep = ( Tardiness_report)new_msg.getObject(); // casting the object to array  [0] = start , [1] = end 
			result = sqlc.tardiness_report(tar_rep); // result will get the table back from the the data base 
			break;
			
		case ACTIVITY_REPORT: // in case that the user want to display the activity report 
			ActivityReport a_r = (ActivityReport)new_msg.getObject(); // casting the object to array  [0] = start , [1] = end 
			result = sqlc.getActivityReportQuery(a_r); // result will get the table back from the the data base 
			break;

		case VIEW_REQUEST://view the current stage and the expected closure stage date
			Integer in=(Integer) new_msg.getObject();
			result = sqlc.viewReq(in.intValue());
			break;
		case SUSPANDING_REQUEST:// change request status to frozen
			Integer in1=(Integer) new_msg.getObject();
			result=sqlc.changeToFrozenQuery(in1.intValue());
			break;
		case EXTENTION_STAGE: //check that user can ask for extention request in case yes the new extension request updated in db  
			int i=1;
			Extention_Stage_req e_s_r=(Extention_Stage_req)new_msg.getObject();
			result=sqlc.CheckExtentionStageQuery(e_s_r);
			break;
		case LOGIN://check that username and user password exist in database
			MsgPackage lgin =  (MsgPackage) new_msg;
			User user=(User) lgin.getObject();
			result=sqlc.loggin_check(user);
			break;
			
		case REGISTER_NEW_USER://check if the user exist if not, the new user will be update in the db
			User user1=(User)new_msg.getObject();
			result=sqlc.add_new_user(user1);
			break;

			
		case GET_EXTENTION_REQ: // in case that the user want to get an extension stage details
			Extention_Stage_req e_s_r1 = (Extention_Stage_req)new_msg.getObject(); // casting the object to Extantion_Stage_req 
			result = sqlc.getExtendedReq(e_s_r1); // result will get the table back from the the data base 
			break;
		
		case CLOSURE_STAGE: // in case that the user want to close the request
			Integer r_id = (Integer)new_msg.getObject(); // casting the object to Integer
			result = sqlc.close_req(r_id); // result will get the table back from the the data base 
			break;
			

		case DETERMINE_DURATION: // in case that the user want to DETERMINE STAGE DURATION
			Stage stage = (Stage)new_msg.getObject(); // casting the object to STAGE
			result = sqlc.insertStageDurationQuery(stage); // result will get the table back from the the data base 
			break;
			
		case ESTIMATION_REPORT: // in case that the user want to set Estimation Report
			EstimationReport e_r= (EstimationReport)new_msg.getObject(); // casting the object to EstimationReport   
			result = sqlc.setEstimationReportQuery(e_r); // result will get the table back from the the data base 
			break;
			
		case EXTENTION_RESOLUTION: // in case that the supervisor want to add his EXTENTION_RESOLUTION 
			Extention_Stage_req ex_r = (Extention_Stage_req)new_msg.getObject(); // casting the object to Extantion_Stage_req  
			result = sqlc.acceptORdeclinedQuery(ex_r); // result will get the table back from the the data base 
			break;
						
			
		case FROZEN_MANAGMENT: // in case that the chifDep want to unfreze a request
			Stage st = (Stage)new_msg.getObject(); // casting the object to Stage 
			result = sqlc.unfreezReq(st.getRequestID()); // result will get the table back from the the data base 
			break;
			
		case INSERT_AUTHORIZATION_SUP_OR_CHAIR: // in case that the chifDep want to determined supervisor or chairman
			User us = (User)new_msg.getObject(); // casting the object to User 
			result = sqlc.determineSupervisorORchairmanQuery(us); // result will get the table back from the the data base 
			break;
			
		case INSERT_AUTHORIZATION_COMM: // in case that the chifDep want to determined committee member
			User us1 = (User)new_msg.getObject(); // casting the object to User 
			result = sqlc.determineCommitteeMember(us1); // result will get the table back from the the data base 
			break;
				
		case UPDATE_AUTHORIZATION_COMM: // in case that the chifDep want to update committee member
			User us2 = (User)new_msg.getObject(); // casting the object to User 
			result = sqlc.updateCommitteeMember(us2); // result will get the table back from the the data base 
			break;
			
		case UPDATE_AUTHORIZATION_SUP_OR_CHAIR: // in case that the chifDep want to update supervisor or chairman
			User us3 = (User)new_msg.getObject(); // casting the object to User 
			result = sqlc.updateCommitteeMember(us3); // result will get the table back from the the data base 
			break;
		case GET_PER:
			User user4 = (User)new_msg.getObject();
			result=sqlc.getReqIds(user4);
			break;
			
				
				
		
			
		
		
		default:
			
			break;
			
			
			
			 
			
		}
		//this.sendToAllClients(result);
		try {
			client.sendToClient(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		}
	

	/**
	 * This method overrides the one in the superclass. Called when the server
	 * starts listening for connections.
	 */
	protected void serverStarted() {
		System.out.println("Server listening for connections on port "
				+ getPort());
	}

	/**
	 * This method overrides the one in the superclass. Called when the server
	 * stops listening for connections.
	 */
	protected void serverStopped() {
		System.out.println("Server has stopped listening for connections.");
	}}

	// Class methods ***************************************************

	

// End of EchoServer class
